﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastReport;
using MINERP.BLL.Statistics;
using MINERP.MODEL.FoundationModel;

namespace MINERP.Statistics.Forms
{
    public partial class FrmOutShopBill : Form
    {
        public FrmOutShopBill()
        {
            InitializeComponent();
        }
        OutShopBillBLL _bll = new OutShopBillBLL();
        private static FrmOutShopBill instance = null;
        public static FrmOutShopBill GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new FrmOutShopBill();

                }
                return instance;
            }
        }

        private void simpleButtonOK_Click(object sender, EventArgs e)
        {
            try
            {
                string productCode = textEditProductBarcode.Text.Trim();
                if (string.IsNullOrEmpty(productCode) || productCode.Equals("0"))
                {

                    if (currShopID != 0)
                    {
                        this.Cursor = Cursors.WaitCursor;

                        Report report = new Report();

                        DataSet set = _bll.GetBillTable(currShopID, dateEditStartTime.Value.Date,
                                                        dateEditEndTime.Value.AddDays(1).Date);

                        this.Cursor = Cursors.WaitCursor;
                        this.simpleButtonOK.Enabled = false;

                        set.WriteXml(@"C:\WINDOWS\xmlData\OutShopBillMain.xml", XmlWriteMode.WriteSchema);

                        FastReport.Report fasrReport = new Report();
                        fasrReport.Preview = previewControl1;
                        fasrReport.Load(Application.StartupPath + "\\Report\\OutShopBillMain.frx");
                        fasrReport.SetParameterValue("ParaStartTime", dateEditStartTime.Value.Date.ToString());
                        fasrReport.SetParameterValue("ParaEndTime", dateEditEndTime.Value.AddDays(1).Date.ToString());
                        this.Cursor = Cursors.Default;
                        this.simpleButtonOK.Enabled = true;
                        previewControl1.Refresh();
                        previewControl1.Show();
                        fasrReport.Show();


                    }
                }
                else
                {
                    if (productCode.Length == 10)
                    {

                    }
                    else
                    {
                        productCode = "2" + productCode.PadLeft(9, '0');
                    }

                    this.Cursor = Cursors.WaitCursor;

                    Report report = new Report();

                    DataSet set = _bll.GetBillTableByOrderProductCode(productCode);
                    if (set != null)
                    {


                        this.Cursor = Cursors.WaitCursor;
                        this.simpleButtonOK.Enabled = false;

                        set.WriteXml(@"C:\WINDOWS\xmlData\OutShopBillMain.xml", XmlWriteMode.WriteSchema);

                        FastReport.Report fasrReport = new Report();
                        fasrReport.Preview = previewControl1;
                        fasrReport.Load(Application.StartupPath + "\\Report\\OutShopBillMain.frx");
                        fasrReport.SetParameterValue("ParaStartTime", dateEditStartTime.Value.Date.ToString());
                        fasrReport.SetParameterValue("ParaEndTime", dateEditEndTime.Value.AddDays(1).Date.ToString());
                        this.Cursor = Cursors.Default;
                        this.simpleButtonOK.Enabled = true;
                        previewControl1.Refresh();
                        previewControl1.Show();
                        fasrReport.Show();
                    }
                    else
                    {
                        MessageBox.Show("产品未找到！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
            catch (Exception exception)
            {

                MessageBox.Show("读取数据失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }

        }

        private void FrmOutShopBill_Load(object sender, EventArgs e)
        {
            comboBoxOutShop.DataSource = _bll.GetShopList();
            comboBoxOutShop.DisplayMember = "OutShopName";
            comboBoxOutShop.ValueMember = "OutShopId";
        }
        private int currShopID = 0;
        private void comboBoxOutShop_SelectionChangeCommitted(object sender, EventArgs e)
        {
            currShopID = ((OutProcessShop_Info)comboBoxOutShop.SelectedItem).OutShopId;
        }

        private void FrmOutShopBill_FormClosing(object sender, FormClosingEventArgs e)
        {
            instance = null;
        }
    }
}
